package com.viettel.rmsmobile;

import com.viettel.BO.PortalUserBean;
import com.viettel.data.LoginFetcher;
import com.viettel.synctask.CandidateSyncTask;
import com.viettel.synctask.LoginSyncTask;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class UserLoginActivity extends Fragment {

	EditText userName;
	EditText password;
	TextView email;
	TextView fullName;
	TextView dateOfBirth;
	TextView sex;
	TextView country;
	Button btnLogin;
	Button btnChangePass;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View loginView = null;
		if (MainActivity.portalUserId == null) {
			loginView = inflater.inflate(R.layout.login_form, container, false);
			userName = (EditText) loginView.findViewById(R.id.login_userName);
			password = (EditText) loginView.findViewById(R.id.login_password);
			btnLogin = (Button) loginView.findViewById(R.id.btnLogin);
			btnLogin.setOnClickListener(new View.OnClickListener() {
		         public void onClick(View v) {
		             actionLogin(v);
		         }
		     });
		} else {
			loginView = inflater.inflate(R.layout.user_information_form,
					container, false);
			email = (TextView) loginView.findViewById(R.id.email);
			fullName = (TextView) loginView.findViewById(R.id.fullName);
			dateOfBirth = (TextView) loginView.findViewById(R.id.dateOfBirth);
			sex = (TextView) loginView.findViewById(R.id.sex);
			country = (TextView) loginView.findViewById(R.id.country);
			btnChangePass = (Button) loginView.findViewById(R.id.btnChangePass);
			btnChangePass.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					changePassWord(v);
				}
			});
			PortalUserBean userBean = MainActivity.portalUser;
			email.setText(userBean.getEmail());
			fullName.setText(userBean.getFullName());
			dateOfBirth.setText(userBean.getDateOfBirth());
			sex.setText(userBean.getSex());
			country.setText(userBean.getCountry());
		}
		return loginView;
	}

	public void viewUserInfo(){
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.content_frame, new UserLoginActivity()).commit();
	}
	/**
	 * Dang nhap he thong
	 * @param view
	 */
	public void actionLogin(View view) {
		LoginSyncTask loginSyncTask = new LoginSyncTask();
		loginSyncTask.execute(this, userName.getText().toString(), password
				.getText().toString());
	}
	
	/**
	 * Doi mat khau
	 * @param v
	 */
	public void changePassWord(View v){
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.content_frame, new ChangePasswordActivity()).commit();
		
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(getActivity(), MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
